package com.example.blog.cofig;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.BuilderDefaults;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //开启swagger2
@EnableKnife4j
public class SwaggerConfig {
    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("用户1");
    }

    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("用户2");
    }

    @Bean
    public Docket docket3() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("用户3");
    }

    //配置了swagger的docket的bean实例
    @Bean
    public Docket docket(Environment environment) {
        //设置要显示的swagger2环境
        Profiles profiles = Profiles.of("dev");

        //通过environment.acceptsProfiles(profiles)判断是否处在自己设定的环境中
        //如果环境是dev,那就是true，如果是prod就是false
        boolean environmentFlag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("苏七")
                .enable(environmentFlag) //是否启用swagger2
                .select() //通过.select()方法，去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                /*RequestHandlerSelectors类中的方法
                basePackages:指定要扫描的包
                any:扫描全部
                none:什么都不扫
                withMethodAnnotation:扫描方法上的注解,比如
                RequestHandlerSelectors.withMethodAnnotation(GetMapping.class)扫描注解带有GetMapping的handler
                扫描类上的注解
                RequestHandlerSelectors.withClassAnnotation(Controller.class)扫描注解带有GetMapping的handler
                * */
                .apis(RequestHandlerSelectors.basePackage("com.example.blog.controller"))
                .build();
    }

    //配置apiInfo
    public ApiInfo apiInfo() {
        /*作者信息*/
        Contact contact = new Contact("苏七", "http://blog.suqiqaq.cn", "1141793961@qq.com");

        return new ApiInfo("blog-api Documentation",
                "博客前端文档",
                "v1.0",
                "http://blog.suqiqaq.cn",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
